package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebRecycleType;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserCardTime;
import com.ruoyi.system.service.IWebRecycleTypeService;
import com.ruoyi.system.service.IWebUserCardTimeService;
import com.ruoyi.system.service.IWebUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 次卡服务Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-27
 */
@Controller
@RequestMapping("/system/userCardTime")
public class WebUserCardTimeController extends BaseController
{
    private String prefix = "system/userCardTime";
    @Autowired
    private IWebUserService iWebUserService;
    @Autowired
    private IWebUserCardTimeService webUserCardTimeService;


    @RequiresPermissions("system:userCardTime:view")
    @GetMapping()
    public String userCardTime()
    {
        return prefix + "/userCardTime";
    }

    /**
     * 查询次卡服务列表
     */
    @RequiresPermissions("system:userCardTime:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebUserCardTime webUserCardTime)
    {
        startPage();
        List<WebUserCardTime> list = webUserCardTimeService.selectWebUserCardTimeList(webUserCardTime);
        return getDataTable(list);
    }

    /**
     * 导出次卡服务列表
     */
    @RequiresPermissions("system:userCardTime:export")
    @Log(title = "次卡服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebUserCardTime webUserCardTime)
    {
        List<WebUserCardTime> list = webUserCardTimeService.selectWebUserCardTimeList(webUserCardTime);
        ExcelUtil<WebUserCardTime> util = new ExcelUtil<WebUserCardTime>(WebUserCardTime.class);
        return util.exportExcel(list, "次卡服务数据");
    }

    /**
     * 新增次卡服务
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        WebUser user = new WebUser();
        List<WebUser> users = iWebUserService.selectWebUserList(user);
        mmap.put("users", JSON.toJSONString(users));
        WebRecycleType webRecycleType = new WebRecycleType();
        webRecycleType.setType(1);
        webRecycleType.setParentId(0L);
        List<WebRecycleType> listRecycleType = iwebRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        mmap.put("listRecycleType",listRecycleType);
        return prefix + "/add";
    }

    /**
     * 新增保存次卡服务
     */
    @RequiresPermissions("system:userCardTime:add")
    @Log(title = "次卡服务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebUserCardTime webUserCardTime)
    {
        webUserCardTime.setTimes(webUserCardTime.getCountTimes());
//        WebUser user = iWebUserService.getById(webUserCardTime.getUserId());
//        if(user != null){
//            webUserCardTime.setDeptId(user.getDeptId());
//        }
        return toAjax(webUserCardTimeService.insertWebUserCardTime(webUserCardTime));
    }

    /**
     * 修改次卡服务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebUserCardTime webUserCardTime = null;
        if(id == null || id ==  -1){
            webUserCardTime = new WebUserCardTime();
        }else{
            webUserCardTime = webUserCardTimeService.selectWebUserCardTimeById(id);
        }
        WebUser user = new WebUser();
        List<WebUser> users = iWebUserService.selectWebUserList(user);
        mmap.put("users", JSON.toJSONString(users));
        WebRecycleType webRecycleType = new WebRecycleType();
        webRecycleType.setType(1);
        webRecycleType.setParentId(0L);
        List<WebRecycleType> listRecycleType = iwebRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        mmap.put("listRecycleType",listRecycleType);
        mmap.put("webUserCardTime", webUserCardTime);
        return prefix + "/edit";
    }

    /**
     * 修改保存次卡服务
     */
    @RequiresPermissions("system:userCardTime:edit")
    @Log(title = "次卡服务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebUserCardTime webUserCardTime)
    {
        return toAjax(webUserCardTimeService.updateWebUserCardTime(webUserCardTime));
    }

    /**
     * 删除次卡服务
     */
    @RequiresPermissions("system:userCardTime:remove")
    @Log(title = "次卡服务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webUserCardTimeService.deleteWebUserCardTimeByIds(ids));
    }

    /**
     * 消费
     * @param webUserCardTime
     * @return
     */
    @PostMapping("/consumption")
    @ResponseBody
    public AjaxResult consumption(WebUserCardTime webUserCardTime){
        return toAjax(webUserCardTimeService.consumption(webUserCardTime , null,null));
    }

    /**
     * 充值
     * @param webUserCardTime
     * @return
     */
    @PostMapping("/chongzhi")
    @ResponseBody
    public AjaxResult chongzhi(WebUserCardTime webUserCardTime){
        return toAjax(webUserCardTimeService.chongzhi(webUserCardTime));
    }

    @Autowired
    private IWebRecycleTypeService iwebRecycleTypeService;
}
